<template>
  <div>
    <nav-bar title="添加联系人"/>
    <div class="wrap">
      <van-address-edit
        :area-list="areaList"
        show-postal
        show-set-default
        :area-columns-placeholder="['请选择', '请选择', '请选择']"
        @save="onSave"
      />
    </div>
  </div>
</template>

<script>
import NavBar from '../../components/NavBar.vue'
import { areaList } from '@vant/area-data'
import { addContact } from '@api'
import { Toast } from 'vant'
export default {
  data () {
    return {
      areaList
    }
  },
  methods: {
    onSave (contact) {
      // 点击保存按钮
      const obj = {}
      for (const key in contact) {
        switch (key) {
          case 'addressDetail':
            obj.address = contact[key]
            break
          case 'areaCode':
            obj.cityId = contact[key]
            break
          case 'name':
            obj.linkMan = contact[key]
            break
          case 'tel':
            obj.mobile = contact[key]
            break
          case 'postalCode':
            obj.provinceId = contact[key]
            break
        }
      }
      obj.isDefault = contact.isDefault
      addContact({
        ...obj,
        token: localStorage.getItem('token')
      }).then(res => {
        if (res.data.code === 0) {
          Toast({
            type: 'success',
            message: '添加成功',
            onClose: () => {
              this.$router.go(0)
            }
          })
        }
      })
    }
  },
  components: {
    NavBar
  }

}
</script>

<style lang="scss" scoped>
.wrap{
  padding-top: 46px;
}
</style>
